package com.flipdf2.serialize.ppml.element;

import java.net.URI;

import org.jdom.Element;
import org.jdom.Namespace;

import com.flipdf2.serialize.ppml.PPMLException;
import com.flipdf2.serialize.ppml.PPMLConstant.PPMLUsage;

public class PPMLExternalDataArray {
	public static final String NAME = "EXTERNAL_DATA_ARRAY";
	
	private URI src;
	private String checksum;
	private String checksumType = "MD5";
	
	private int index = 1;
	private String indexUsage = PPMLUsage.UNKNOW;
	
	public Element toXML(Namespace defaultNamespace) {
		Element root = null;
		if (defaultNamespace != null) {
			root = new Element(NAME, defaultNamespace);
		} else {
			root = new Element(NAME);	
		}
		
		if (src != null) 
			root.setAttribute("Src", src.toString());
		if (checksum != null) 
			root.setAttribute("Checksum", checksum);
		if (checksumType != null) 
			root.setAttribute("ChecksumType", checksumType);
		if (index > 0)
			root.setAttribute("Index", index + "");
		if (indexUsage != null) 
			root.setAttribute("IndexUsage", indexUsage);
		
		return root;
	}

	public void check() throws PPMLException {
		if (src == null) {
			throw PPMLException.getInstance(PPMLException.REQUIRED_ATTRIBUTE_LOST, "Src");
		}
	}
	
	public String getChecksum() {
		return checksum;
	}

	public void setChecksum(String checksum) {
		this.checksum = checksum;
	}

	public String getChecksumType() {
		return checksumType;
	}

	public void setChecksumType(String checksumType) {
		this.checksumType = checksumType;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String getIndexUsage() {
		return indexUsage;
	}

	public void setIndexUsage(String indexUsage) {
		this.indexUsage = indexUsage;
	}

	public URI getSrc() {
		return src;
	}

	public void setSrc(URI src) {
		this.src = src;
	}
}
